﻿using System.Threading.Tasks;
using SpotOffer.CustomViews;
using SpotOffer.Resource;
using Xamarin.Forms;
using XLabs.Forms.Controls;

namespace SpotOffer.View
{
    public class ChooseCityPage : BasePage
    {
        public ExtendedLabel LabelChooseCity;
        public ListView ListViewChooseCity;
        public RelativeLayout ChooseCityLayout { get; set; }

        protected override void OnAppearing()
        {
            base.OnAppearing();
            Title = AppResources.ChooseCity;
            ChooseCityLayout = new RelativeLayout {BackgroundColor = AppColors.White};

            ListViewChooseCity = AppViews.ReturnListView();
            ListViewChooseCity.BackgroundColor = AppColors.White;
            ListViewChooseCity.SetBinding(ListView.ItemsSourceProperty, new Binding("CityList"));
            ListViewChooseCity.SetBinding(ListView.SelectedItemProperty, new Binding("SelectedCity"));
            ListViewChooseCity.ItemTemplate = new DataTemplate(() =>
            {
                var settingLabel = AppViews.ItemLabel();
                settingLabel.SetBinding(Label.TextProperty, "name");
                settingLabel.SetBinding(Label.TextColorProperty, "ItemTextColor");

                var finalview = AppViews.ItemContentView(settingLabel);
                finalview.SetBinding(BackgroundColorProperty, "ItemBackgroundColor");

                return new ViewCell
                {
                    View = finalview
                };
            });
            ListViewChooseCity.ItemSelected += (s, e) =>
            {
                Navigation.PopAsync(true).ContinueWith((sender) =>
                 {
                     Task.Delay(200).ContinueWith((ss) =>
                     {
                         Device.BeginInvokeOnMainThread((() =>
                         {
                             App.AppInstance.MasterPage.IsPresented = true;
                         }));

                     });
                 });
              
              
            };

            ChooseCityLayout.Children.Add(ListViewChooseCity, Constraint.Constant(0), Constraint.Constant(0), Constraint.RelativeToParent((parent) => parent.Bounds.Width));// Constraint.RelativeToView(header, (par, sib) => sib.Bounds.Bottom));

            Content = ChooseCityLayout;
        }

  


    }
}
